;;; +edit.el --- update edit process -*- lexical-binding: t -*-

;;; Commentary:

;;; Code:

;; -------- bind & unbind --------

;; remap "C-h"
;; via: Chris
;; (define-key key-translation-map (kbd "C-h") "")
;; via: twlz0ne
;; (global-set-key (kbd "<f1>") 'help-command)

(global-set-key (kbd "<f1> c") 'describe-char)

(global-set-key (kbd "C-r") 'replace-string)
(global-set-key (kbd "C-S-r") 'query-replace-regexp)

;; -------- unbind unneeded keys, via: MatthewZMD
(global-set-key (kbd "C-z") nil)
(global-set-key (kbd "M-z") nil)
(global-set-key (kbd "C-x C-z") nil)

;; set DOUBLE `C-@' as `set-rectangular-region-anchor'(github.com/magnars/multiple-cursors.el#special)
;; via: emacs-china.org/t/c-set-mark-command-c-c-multiple-cursors-set-rectangular-region-anchor/17889
(add-hook 'activate-mark-hook
          (lambda ()
            (local-set-key (kbd "C-@") 'set-rectangular-region-anchor)))
(add-hook 'deactivate-mark-hook
          (lambda ()
            (local-unset-key (kbd "C-@"))))

;; -------- switch modes keybindings --------
(defhydra hydra-cor5-switch-multi-modes ()
  ("e" emacs-lisp-mode "Elisp" :exit t)
  ("l" LaTeX-mode "LaTeX" :exit t)
  ("m" gfm-mode "M↓" :exit t)
  ("r" ess-r-mode "R" :exit t))
(global-set-key (kbd "C-c M") 'hydra-cor5-switch-multi-modes/body)

;; -------- awesome-pair --------
(use-package awesome-pair
  :load-path "lazycat-bucket/awesome-pair"
  :if (file-exists-p
       (expand-file-name "lazycat-bucket/awesome-pair/awesome-pair.el"
                         user-emacs-directory))
  :defer t
  :hook
  ((prog-mode conf-mode gfm-mode markdown-mode org-mode text-mode yaml-mode) . awesome-pair-mode)
  :bind
  (("(" . 'awesome-pair-open-round)
   ("[" . 'awesome-pair-open-bracket)
   ("{" . 'awesome-pair-open-curly)
   (")" . 'awesome-pair-close-round)
   ("]" . 'awesome-pair-close-bracket)
   ("}" . 'awesome-pair-close-curly)
   ("=" . 'awesome-pair-equal)
   ("%" . 'awesome-pair-match-paren)
   ("\"" . 'awesome-pair-double-quote)
   ("SPC" . 'awesome-pair-space)
   ("RET" . 'awesome-pair-newline)
   ("DEL" . 'awesome-pair-backward-delete)
   ("C-d" . 'awesome-pair-forward-delete)
   ("C-k" . 'awesome-pair-kill)
   ("M-k" . cor5-awesome-pair-copy)
   ("M-\"" . 'awesome-pair-wrap-double-quote)
   ("M-[" . 'awesome-pair-wrap-bracket)
   ("M-{" . 'awesome-pair-wrap-curly)
   ("M-(" . 'awesome-pair-wrap-round)
   ("M-s" . 'awesome-pair-unwrap)
   ("C-M-f" . 'awesome-pair-jump-right)
   ("C-M-b" . 'awesome-pair-jump-left)
   ("C-<return>" . 'awesome-pair-jump-out-pair-and-newline))
  :config
  (defun cor5-awesome-pair-copy ()
    "Combine `awesome-pair-kill' & `yank' to instead `thing-copy-to-line-end'."
    (interactive)
    (let ((pt (point)))
      (awesome-pair-kill)
      (yank)
      (goto-char pt)))
  (remove-hook 'awesome-pair-mode-map 'SAS-mode))

;; -------- use thing-edit to cut/copy quickly --------
(use-package thing-edit
  :load-path "lazycat-bucket/thing-edit"
  :if (file-exists-p
       (expand-file-name "lazycat-bucket/thing-edit/thing-edit.el"
                         user-emacs-directory))
  :defer t
  :config
  (defun cor5-thing-cut-region-or-symbol ()
    "Cut content of the current region or symbol around cursor."
    (interactive)
    (if (region-active-p)
        (thing-cut-region-or-line)
      (thing-cut-symbol)))
  (defun cor5-thing-copy-region-or-symbol ()
    "Copy content of the current region or symbol around cursor."
    (interactive)
    (if (region-active-p)
        (thing-copy-region-or-line)
      (thing-edit 'symbol)))
  :bind
  (("C-M-w" . thing-copy-whole-buffer)
   ("C-S-y" . thing-replace-symbol)
   ("C-w" . cor5-thing-cut-region-or-symbol)
   ("M-w" . cor5-thing-copy-region-or-symbol)))

;; use `expand-region' to mark region
(use-package expand-region
  :bind
  (("M-'" . er/mark-inside-pairs)
   ("M-]" . er/expand-region)
   ;; ("M-[" . er/contract-region)
   ))

;; -------- undo-tree --------
(use-package undo-tree
  :defer t
  :diminish undo-tree-mode
  :hook
  (after-init . global-undo-tree-mode)
  :config
  (setq undo-tree-visualizer-diff t
        undo-tree-auto-save-history nil
        undo-tree-enable-undo-in-region nil
        ;; Increase undo-limits by a factor of ten to avoid emacs prematurely
        ;; truncating the undo history and corrupting the tree. See
        ;; https://github.com/syl20bnr/spacemacs/issues/12110
        undo-limit 800000
        undo-strong-limit 12000000
        undo-outer-limit 120000000)
  :bind
  (:map undo-tree-map
        ("M-_" . nil)))

(provide '+edit)

;;; +edit.el ends here
